using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JugglingMvc.Models;
using JugglingMvc.Helpers;
using JugglingMvc.Models.Jugglers;

namespace JugglingMvc.Controllers
{
    public class JugglersController : Controller
    {
        // GET: /Jugglers/
        JugglersRepository jugglersRepository = new JugglersRepository();

        public ActionResult Index(int? page)
        {
            const int pageSize = 5;
            var jugglers = jugglersRepository.FindAllJugglers();
            var paginatedJugglers = new PaginatedList<Juggler>(jugglers, page ?? 0, pageSize);

            ViewData.Model = paginatedJugglers;

            if (HttpContext.Request.IsAjaxRequest())
                return PartialView("List");
            else
                return View("Index");
        }



        public ActionResult Details(int id)
        {
            Juggler juggler = jugglersRepository.GetJuggler(id);
            if (juggler == null)
                return View("NotFound");
            else
            {
                JugglersDetailsModel viewModel = new JugglersDetailsModel { Juggler = juggler };

                return View(viewModel);
            }
        }

        public ActionResult Edit(int id)
        {
            Juggler juggler = jugglersRepository.GetJuggler(id);
            var viewModel = new JugglersFormViewModel();
            fillViewModel(viewModel, juggler);
            return View(viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {

            Juggler juggler = jugglersRepository.GetJuggler(id);
            try
            {
                UpdateModel(juggler);
                jugglersRepository.Save();
                return RedirectToAction("Details", new { id = juggler.Id });
            }
            catch
            {
                foreach (var issue in juggler.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                var viewModel = new JugglersFormViewModel();
                fillViewModel(viewModel, juggler);
                return View(viewModel);
            }
        }

        private void fillViewModel(JugglersFormViewModel vm, Juggler juggler)
        {
            vm.Juggler = new JugglerData
                    {
                        BornDay = juggler.BornDay,
                        Id = juggler.Id,
                        Name = juggler.Name,
                        SpecialityId = juggler.SpecialtyId.ToString(),
                        TeamId = juggler.TeamId.ToString(),
                        WebPage = juggler.WebPage

                    };
            vm.Teams = new SelectList(
                        jugglersRepository.GetTeams(),
                        "Id",
                        "Name",
                        juggler.TeamId.ToString()
                        );
            vm.Specialities = new SelectList(
                        jugglersRepository.GetSpecialities(),
                        "Id",
                        "Name",
                        juggler.SpecialtyId.ToString()
                    );
        }

        // GET: /Jugglers/Create
        public ActionResult Create()
        {
            int nextId = 0;
            if (jugglersRepository.FindAllJugglers().Count() != 0)
            {
                nextId = jugglersRepository.FindAllJugglers().ToList().Last().Id + 1;
            }
            Juggler juggler = new Juggler() { Id = nextId };

            var viewModel = new JugglersFormViewModel();
            fillViewModel(viewModel, juggler);
            return View(viewModel);
        }

        //
        // POST: /Jugglers/Create
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(JugglerData jugglerData)
        {
            var juggler = new Juggler
            {
                BornDay = jugglerData.BornDay,
                Id = jugglerData.Id,
                Name = jugglerData.Name,
                SpecialtyId = int.Parse(jugglerData.SpecialityId),
                TeamId = (jugglerData.TeamId != null) ? int.Parse(jugglerData.TeamId) : null,
                WebPage = jugglerData.WebPage
            };

            if (ModelState.IsValid)
            {
                try
                {
                    jugglersRepository.Add(juggler);
                    jugglersRepository.Save();
                    return RedirectToAction("Details", new { id = juggler.Id });
                }
                catch
                {
                    foreach (var issue in juggler.GetRuleViolations())
                    {
                        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                    }
                }
            }
            return View(juggler);
        }

        public ActionResult Delete(int id)
        {
            Juggler juggler = jugglersRepository.GetJuggler(id);
            if (juggler == null)
                return View("NotFound");
            else
                return View(juggler);
        }


        //
        // HTTP POST: /Dinners/Delete/1
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            Juggler juggler = jugglersRepository.GetJuggler(id);
            if (juggler == null)
                return View("NotFound");
            jugglersRepository.Delete(juggler);
            jugglersRepository.Save();
            TempData["alert"] = "borrose";
            return RedirectToAction("index");
        }

    }


}




